package cz.muni.fi.pa036.kiss.Entity;

import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author peter
 */
@Entity
@Table(name="UBYTOVANI")
public class Ubytovani implements Cloneable, Serializable{
    private Long id;
    private Kolej kolej;
    private Pokoj pokoj;
    private Student student;
    private Date datumOd;
    private Date datumDo;
    private Double mesicniSazba;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="ID_UBYTOVANI")
    public Long getId() {
        return id;
    }

    public Ubytovani setId ( final Long id ) {

        if ( id != null ) {

            this.id = new Long ( id.longValue () );
        }

        return this;
    }

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="ID_KOLEJ")
    public Kolej getKolej() {
        return kolej;
    }

    public void setKolej(Kolej kolej) {
        this.kolej = kolej;
    }
    
    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="ID_POKOJ")
    public Pokoj getPokoj() {
        return pokoj;
    }

    public void setPokoj(Pokoj pokoj) {
        this.pokoj = pokoj;
    }
    
    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="ID_STUDENT")
    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    @Temporal(TemporalType.DATE)
    @Column(name="DATUM_OD")
    public Date getDatumOd() {
        return datumOd;
    }

    public void setDatumOd(Date datumOd) {
        this.datumOd = datumOd;
    }

    @Temporal(TemporalType.DATE)
    @Column(name="DATUM_DO",insertable = false, updatable = false)
    public Date getDatumDo() {
        return datumDo;
    }

    public void setDatumDo(Date datumDo) {
        this.datumDo = datumDo;
    }

    @Column(name="MESICNI_SAZBA")
    public Double getMesicniSazba() {
        return mesicniSazba;
    }

    public void setMesicniSazba(Double mesicniSazba) {
        this.mesicniSazba = mesicniSazba;
    }

    @Override
    public String toString() {
        return "Ubytovani{" + "id=" + id + ", kolej=" + kolej + ", pokoj=" + pokoj + ", student=" + student + ", datumOd=" + datumOd + ", datumDo=" + datumDo + ", mesicniSazba=" + mesicniSazba + '}';
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 47 * hash + Objects.hashCode(this.id);
        hash = 47 * hash + Objects.hashCode(this.kolej);
        hash = 47 * hash + Objects.hashCode(this.pokoj);
        hash = 47 * hash + Objects.hashCode(this.student);
        hash = 47 * hash + Objects.hashCode(this.datumOd);
        hash = 47 * hash + Objects.hashCode(this.datumDo);
        hash = 47 * hash + Objects.hashCode(this.mesicniSazba);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Ubytovani other = (Ubytovani) obj;
        if (!Objects.equals(this.id, other.id)) {
            return false;
        }
        if (!Objects.equals(this.kolej, other.kolej)) {
            return false;
        }
        if (!Objects.equals(this.pokoj, other.pokoj)) {
            return false;
        }
        if (!Objects.equals(this.student, other.student)) {
            return false;
        }
        if (!Objects.equals(this.datumOd, other.datumOd)) {
            return false;
        }
        if (!Objects.equals(this.datumDo, other.datumDo)) {
            return false;
        }
        if (!Objects.equals(this.mesicniSazba, other.mesicniSazba)) {
            return false;
        }
        return true;
    }
    
    @Override
    public Object clone () {

        try {

            return ( (Ubytovani) super.clone () ).setId ( this.id );
        } catch ( CloneNotSupportedException ex ) {

            assert false: "CloneNotSupportedException thrown";
            return null;
        }
    }
    
    public void checkState () {

        final boolean noDateOd = this.datumOd == null;
        final boolean noDateDo = this.datumDo == null;
        final boolean noSazba = this.mesicniSazba == null;

        if ( noDateOd && noDateDo && noSazba )  {

            throw new IllegalStateException ( "Datum nastupu, datum odchodu a mesicni sazba musi byt nastaveny" );
        }
  }
    
}
